{% extends "base/base_width_left.html" %}
{% load pagination_tags %}
{% block style %}
    <link rel="stylesheet" href="/static/bootstrap3_datetime/css/bootstrap-datetimepicker.css"/>
{% endblock %}
{% block flag %}log{% endblock %}
{% block path %}
    {% if server %}
        <a href="{% url 'server:detail' pk=server.id%}" title="" class="tip-bottom">{{ server.ip }}详情</a>
    {% endif %}
    <a href="" title="" class="tip-bottom">{% if server %}{{ server.ip }}日志查看{% else %}日志查看{% endif %}</a>
{% endblock %}

{#{% block searchname %}searchaction{% endblock %}#}
{#{% block searchtip %}订单编号{% endblock %}#}

{% block container %}
    {{ block.super }}
        {% autopaginate operation_list 10%}
    {#    <ul class="nav nav-tabs">#}
    {#        <li role="presentation" class="active"><a href="{% url 'product:product_list' %}">订单</a></li>#}
    {#        <li role="presentation"><a href="{% url 'product:type_list' %}">分 类</a></li>#}
    {#        <li role="presentation"><a href="{% url 'product:theme_list' %}">主题</a></li>#}
    {#    </ul>#}
    <div hidediv="hide" class="center-block">
        <form method="get" id="search_form">
            <input hidden value="" name="status" id="status"/>
            <input hidden value="{{ server.id }}" name="sid" id="status"/>
            <div class="dropdown" style="display:inline">
                <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown"
                                    aria-expanded="true">
                                {{ status_show }}
                                <span class="caret"></span>
                            </button>
                <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
                    <li role="presentation">
                        <a role="menuitem" tabindex="-1" href="{% if server.id %}{% url 'operation:list' %}?sid={{ server.id }}{% else %}{% url 'operation:list' %}{% endif %}">全部</a>
                    </li>
                    {% for status in status_tuple %}
                        <li role="presentation">
                            <a role="menuitem" tabindex="-1" status="{{  status.0  }}"
                               href="{% if server.id %}{% url 'operation:list' %}?sid={{ server.id }}&status={{ status.0 }}{% else %}{% url 'operation:list' %}?status={{ status.0 }}{% endif %}">{{ status.1 }}</a>
                        </li>
                    {% endfor %}
                </ul>

                <input class="input-sm" name="searchaction" placeholder="输入服务器ip/剧本名进行搜索"/>
                <div>
                    <div class="input-group date" id="start_time" style="width: 30%;float: left;">
                        <input class="form-control"  name="start_time" placeholder="开始时间">
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                    <span style="float: left;font-size: 25px">----</span>
                    <div class="input-group date" id="end_time" style="width: 30%;float: left;">
                        <input class="form-control"  name="end_time" placeholder="结束时间">
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                    <button type="button" class="btn btn-default" id="search_btn">搜索</button>
                    <div style="clear: both;"></div>
                </div>

            </div>
            {#        <a href="{% url 'product:product_form' %}" class="btn btn-primary">添 加</a>#}
        </form>
    </div>
    <div hidediv="hide" class="table-responsive">
        <form id="log_form" method="post">
        {% csrf_token %}
        <table class="table table-hover table-bordered ">
            <thead>
            <tr>
                <th>
                    <input type="checkbox" class="all">
                    <button class="btn btn-default run_re" type="button"
                               url="{% url 'playbooks:replay' %}">执行重载
                            </button>
                </th>
                <th>剧本名</th>
                <th>对应的服务器</th>
                <th>剧本版本</th>
                <th>开始时间</th>
                <th>结束时间</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            {% for operation in operation_list %}
                <tr>
                    <td>
                        <input type="checkbox" value="{{ operation.server.id }}" name="sid"/>
                    </td>
                    <td>
                        {% if operation.books_name %}
                        {{ operation.books_name }}
                            <input hidden type='checkbox' value="{{ operation.play_books.id }}" name="pbid"/>
                        {% else %}
                            更新操作
                            <input hidden type='checkbox' value="更新" name="pbid"/>
                        {% endif %}
                    </td>
                    <td>
                        {% if operation.server %}
                        {{ operation.server.ip }}

                        {% else %}
                        {{ operation.server_ip }}
                        {% endif %}
                    </td>
                    <td>
                        {% if operation.version %}
                        {{ operation.version }}
                           <input hidden type='checkbox' value="{{ operation.version }}" name="version"/>
                        {% else %}
                            未知
                            <input hidden type='checkbox' value="未知" name="version"/>
                        {% endif %}
                    </td>
{#                    <td>{{ operation.result }}</td>#}
                    <td>{{ operation.create_time|date:'Y-m-d H:i:s' }}</td>
                    <td>{{ operation.end_time|date:'Y-m-d H:i:s' }}</td>
                    <td><span style="color:{% if operation.status == 'success' %}red{% endif %}">{{ operation.get_status_display }}</span></td>
                    <td>
                        <a class="btn btn-default btn-sm"
                                href="{% url 'operation:detail' pk=operation.id%} ">查看详情
                        </a>
                        <button type="button" class="btn btn-default btn-sm run" sid="{{ operation.server.id }}" version="{{ operation.version }}"
                                url="{% url 'playbooks:run'%}?pbid={{ operation.play_books.id }} ">重新执行
                        </button>
                        <button type="button" class="btn deletebtn btn-default btn-sm"
                                deleteurl="{% url 'operation:delete' oid=operation.id%} ">删除
                        </button>
                    </td>
                </tr>
            {% empty %}
                <tr>
                    <td colspan="8"><p>没有查询到相关信息.</p></td>
                </tr>
            {% endfor %}
        </table>
     {% paginate %}
        </form>
{#        <div class="pagination">#}
{#            <span class="step-links">#}
{#                {% if page_obj.has_previous %}#}
{#                    <a href="?page={{ page_obj.prevous_page_number }}">上一页</a>#}
{#                {% endif %}#}
{#                <span class="current">#}
{#                    page {{ page_obj.number }} of {{ paginator.num_pages }}.#}
{#                </span>#}
{#                {% if page_obj.has_next %}#}
{##}
{#                    <a href="?page={{ page_obj.next_page_number }}">下一页</a>#}
{#                {% endif %}#}
{#            </span>#}
{#        </div>#}

    </div>
{% endblock %}
{% block footjs %}
    <script type="text/javascript" src="/static/bootstrap3_datetime/js/moment.min.js"></script>
    <script type="text/javascript" src="/static/bootstrap3_datetime/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
    <script type="text/javascript" src="/static/bootstrap3_datetime/js/bootstrap-datetimepicker.js"></script>
    <script>
        $.ajaxSetup({
                    data: {csrfmiddlewaretoken: '{{ csrf_token }}'}
                });
        //重新运行脚本
        $('.run').on('click',function(){
            var sid=$(this).attr('sid');
            var version=$(this).attr('version');
            var url=$(this).attr('url');
            $.post(url,{'sid':sid,'version':version},function(data){
                if(data==1){
                    alert("正在运行剧本");
                    time_lazy();
                }else{
                    alert('系统错误，无法运行')
                }
            })
        });
        $('.all').on('click', function () {
            if ($(this).is(':checked')) {
                $('td').find('input[name="sid"]').prop('checked', true);
                $('td').find('input[name="pbid"]').prop('checked', true);
                $('td').find('input[name="version"]').prop('checked', true);
                {#                alert($('td').find('input[name="sid"]').attr('checked'))#}
            } else {
                $('td').find('input[name="sid"]').removeAttr('checked');
                $('td').find('input[name="pbid"]').removeAttr('checked');
                $('td').find('input[name="version"]').removeAttr('checked');
            }
        });
        //批量重新运行脚本
        $('.run_re').on('click', function () {
            var $form = $('#log_form');
            if(is_server()){
                var url = $(this).attr('url');
                $.post(url, $form.serialize(), function (data) {
                    if (data == 1) {
                        alert('剧本已运行');
                        time_lazy()
                    } else {
                        alert('运行失败');
                    }
                })
            }else{
                alert('请选择一个日志！');
            }
        });
        function is_server(){
            var $sid_list=$("input[name='sid']");
            for(var i=0;i<$sid_list.length;i++){
                if($sid_list.eq(i).is(':checked')){
                    return true
                }
            }
            return false
        }
        $('input[name="sid"]').on('click',function(){
           if($(this).is(':checked')){
               $(this).parents('tr').find('input[name="pbid"]').prop('checked', true);
               $(this).parents('tr').find('input[name="version"]').prop('checked', true);
           }else{
               $(this).parents('tr').find('input[name="pbid"]').removeAttr('checked');
               $(this).parents('tr').find('input[name="version"]').removeAttr('checked');
           }
        });
    //时间控件
    $(function(){
        $('#start_time').datetimepicker({"pickSeconds":true,"format":"YYYY-MM-DD HH:mm:ss","language":"zh-Has","pickDate":true})
        $('#end_time').datetimepicker({"pickSeconds":true,"format":"YYYY-MM-DD HH:mm:ss","language":"zh-Has","pickDate":true})

    });
    $('#search_btn').on('click',function(){
        var status_show="{{ status_show }}";
        var $status_list=$(".dropdown-menu").find('li');
        for(var i=0;i<$status_list.length;i++){
            //alert($status_list.eq(i).find('a').text());
            if(String($status_list.eq(i).find('a').text())==status_show){
                $("#status").val($status_list.eq(i).find('a').attr('status'));
                $("#search_form").submit();
            }
        }
        $("#search_form").submit();
    });

    </script>
{% endblock %}
